package com.mdlib.helper.module.send.profile.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.entity.BuyEntity;
import com.mdlib.helper.ui.adapter.SendBuyAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/12/19 13:37
 * 邮箱：hqy_xz@126.com
 */

public class SendBuySuccessFragment extends BaseFragment {
    @Bind(R.id.rv_buy_success)
    RecyclerView mRvBuySuccess;
    @Bind(R.id.sv_buy_success)
    SpringView mSvBuySuccess;
    private SendBuyAdapter mSendBuyAdapter;
    private List<BuyEntity> buyList = new ArrayList<>();

    private int pageNumber = 1;

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_send_buy_success;
    }


    private void init() {
        mSendBuyAdapter = new SendBuyAdapter(buyList);
        mRvBuySuccess.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvBuySuccess.setAdapter(mSendBuyAdapter);

        mSvBuySuccess.setType(SpringView.Type.FOLLOW);
        mSvBuySuccess.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                buyList.clear();
                pageNumber = 1;
                getBuySuccess();
            }

            @Override
            public void onLoadmore() {
                getBuySuccess();
            }
        });
        mSvBuySuccess.setHeader(new HelperHeads(getActivity()));
        mSvBuySuccess.setFooter(new DefaultFooter(getActivity()));
        getBuySuccess();
    }


    private void getBuySuccess() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getSendMyBuy(pageNumber + "", "done")
                .compose(RxHelper.<ArrayList<BuyEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<BuyEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<BuyEntity> buyEntities) {
                        pageNumber++;
                        updateList(buyEntities);
                        mSvBuySuccess.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                        mSvBuySuccess.onFinishFreshAndLoad();
                    }
                }));
    }

    private void updateList(ArrayList<BuyEntity> buyEntities) {
        if (buyEntities.size() > 0 && pageNumber >= 2) {
            for (BuyEntity buyEntity : buyEntities) {
                buyList.add(buyEntity);
            }
            mSendBuyAdapter.notifyDataSetChanged();
        } else {
            pageNumber--;
            ToastUtil.showToast("完成没有更多数据了!");
        }
    }
}
